from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
#基类
class Base():
    #初始化
    def __init__(self,driver):
        self.driver=driver
    #打开网址
    def open_url(self,url):
        self.driver.get(url)
    #封装定位
    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20,1).until(lambda driver: driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            print(e)
    #点击
    def click(self,loc):
        self.find_element(loc).click()
    #输入
    def send_keys(self,loc,text):
        self.find_element(loc).send_keys(text)
    #下滑任意像素
    def window_scrollTo(self,x):
        self.driver.execute_script(f'window.scrollTo(0,{x})')
    #新窗口
    def window_handles(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])
    #下拉
    def Select_value(self,loc,n):
        Select(self.find_element(loc)).select_by_value(n)
    def Select_index(self, loc, n):
        Select(self.find_element(loc)).select_by_index(n)
    def Select_text(self, loc, n):
        Select(self.find_element(loc)).select_by_visible_text(n)
    #鼠标 双击
    def action_chains_click(self,loc):
        ActionChains(self.driver).double_click(self.find_element(loc)).perform()
    #鼠标 悬停
    def action_chains_xt(self, loc):
        ActionChains(self.driver).move_to_element(self.find_element(loc)).perform()
    # 键盘 a
    def keys_a(self,loc):
        self.find_element(loc).send_keys(Keys.CONTROL,'a')
    # 键盘 c
    def keys_c(self, loc):
        self.find_element(loc).send_keys(Keys.CONTROL,'c')
    # 键盘 x
    def keys_x(self, loc):
        self.find_element(loc).send_keys(Keys.CONTROL,'x')

